<?php

/**
 * This file is part of the Codizer package.
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 *
 * @copyright   Copyright (c) 2010 Advisa (http://www.advisa.fr)
 * @author      Codizer Core Team <team@codizer.org>
 * @package     Codizer
 */

class f_locale_Manager
{
	private static $instance;
	
	/**
	 * @return f_locale_Manager
	 */
	private function __construct()
	{
		return $this;
	}
	
	/**
	 * Get singleton instance
	 *
	 * @return f_locale_Manager
	 */
	public static function getInstance()
	{
		if(self::$instance === null)
		{
			self::$instance = new self();
		}
		return self::$instance;
	}

	/**
	 * Get a locale from an xml file
	 *
	 * @param string $lang
	 * @param string $key
	 * @param array $options
	 * @return string locale
	 */
	public static function getLocaleFromXML($file, $key, $lang = null, $values = null, $default = null)
	{
        $locale = !is_null($default) ? $default : null;

        if(is_null($lang))
		{
			$lang = f_core_Context::getInstance()->getDisplayLang();
		}

        if(is_file($file))
        {
            $el = f_xml_Xpath::getInstance($file)->fromXpath('locale[@key="'.$key.'"]/content[@lang="'.$lang.'"]')->getElement();

            if(is_null($el))
            {
                $el = f_xml_Xpath::getInstance($file)->fromXpath('locale[@key="'.$key.'"]/content[@lang="en"]')->getElement();    
            }

            if(!is_null($el))
            {
                $locale = $el->getContent();

                // If there is template values
                if(!is_null($values) && is_array($values))
                {
                    foreach($values as $key => $value)
                    {
                        $locale = str_replace("{".$key."}", $value, $locale);
                    }
                }
            }

            return $locale;
        }
        else
        {
            return null;
        }
	}
	
	/**
	 * Get locale from module locale file
	 * Can use some template values
	 * 
	 * @example locale = "My name is {name}" -> getLocale("locale.module.filename.key", "EN", array("name" => "Sebastien")) -> "My name is Sebastien"
	 *
	 * @param string $key
	 * @param string $lang
	 * @param array $values
	 * @return string locale
	 */
	public static function getLocale($key, $lang = null, $values = null, $default = null)
	{
		if(substr($key, 0, 7) != "locale.")
		{
			$key = "locale.".$key;
		}

        list($prefix, $module, $file, $key) = explode('.', $key);
		
        $filePath = MODULES_DIR.DS.$module.DS.'locales'.DS.$file.'.xml';
        $locale = self::getLocaleFromXML($filePath, $key, $lang, $values, $default);

        if(is_null($locale))
        {
            $filePath = WEBAPP_MODULES_DIR.DS.$module.DS.'locales'.DS.$file.'.xml';
            $locale = self::getLocaleFromXML($filePath, $key, $lang, $values, $default);
        }

        return $locale;
    }

    /**
     * Get culture label from culture code
     * @example fr_FR -> français (France)
     *
     * @param string $culture
     * @return string
     */
    public static function getCultureLabel($culture)
    {
        $xml = f_xml_Xpath::getInstance(FRAMEWORK_HOME.DS.'locale'.DS.'xml'.DS.'cultures.xml');

        $el = $xml->fromXpath('culture[@id="'.$culture.'"]')->getElement();

        return is_null($el) ? $culture : $el->getContent();
    }

    /**
     *
     * @return f_xml_XmlElement
     */
    public static function getIsoCodes()
    {
        $xml = f_xml_Xpath::getInstance(FRAMEWORK_HOME.DS.'locale'.DS.'xml'.DS.'isocodes.xml');

        $els = $xml->fromXpath('language')->getElements();

        $result = array();

        foreach($els as $k => $el)
        {
            $res['iso_639_1'] = $el->getSubElement('iso_639_1')->getContent();
            $res['iso_639_2'] = $el->getSubElement('iso_639_2')->getContent();
            $res['label_fr'] = $el->getSubElement('label_fr')->getContent();
            $res['label_en'] = $el->getSubElement('label_en')->getContent();
            $res['label_orig'] = $el->getSubElement('label_orig')->getContent();
            $label_en[$k] = $res['label_en'];
            $result[] = $res;
        }

        array_multisort($label_en, SORT_ASC, $result);

        return $result;
    }

    /**
     *
     * @param string $isoCode
     * @return f_xml_XmlElement
     */
    public static function getDatasFromIso($isoCode)
    {
        $isoCode = strtoupper($isoCode);

        $xml = f_xml_Xpath::getInstance(FRAMEWORK_HOME.DS.'locale'.DS.'xml'.DS.'isocodes.xml');

        $el = $xml->fromXpath('language[iso_639_1="'.$isoCode.'" or iso_639_2="'.$isoCode.'"]')->getElement();

        if(!is_null($el))
        {
            $result['iso_639_1'] = $el->getSubElement('iso_639_1')->getContent();
            $result['iso_639_2'] = $el->getSubElement('iso_639_2')->getContent();
            $result['label_fr'] = $el->getSubElement('label_fr')->getContent();
            $result['label_en'] = $el->getSubElement('label_en')->getContent();
            $result['label_orig'] = $el->getSubElement('label_orig')->getContent();
            return $result;
        }

        return null;
    }
}